package com.vchello.controller.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vchello.bean.security.UserRole;
import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.service.security.UserRoleService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/userRole")
public class UserRoleController extends BaseController {
	
	@Autowired
	private UserRoleService userRoleService;

	
	public String list(){
		return null;
		
	}
	
	@RequestMapping(value = "/childerList" , method = RequestMethod.POST)
	public void childerList(){
		User user = getUser();
		if(null == user){
			error("用户未登录");
			return;
		}
		List<UserRole> userRoleList = userRoleService.findUsreRoleByFatherId(user.getId());
		JSONArray jsonArray = new JSONArray();
		for (UserRole userRole : userRoleList) {
			JSONObject json = new JSONObject();
			json.put("id", userRole.getUserId());
			json.put("name", userRole.getUser().getUserName());
			jsonArray.add(json);
		}
		
		success(jsonArray.toString());
		
	}
	
	@RequestMapping(value = "/getRole" , method = RequestMethod.POST)
	public void getRole(){
		User user = getUser();
		if(null == user){
			error("用户未登录");
			return;
		}
	}
	
	@RequestMapping(value = "/updateUserRoel" , method = RequestMethod.POST)
	public void updateUserRoel(int roleId, int userId){
		User user =getUser();
		if(null == user) {
			error("用户未登录");
			return;
		}
		UserRole userRole = userRoleService.findUserRole(userId);
		if(null == userRole) {
			error("找不到该用户角色");
			return;
		}
		userRole.setRoleId(roleId);
		boolean isTrue = userRoleService.updateUserRole(userRole);
		if(isTrue) {
			success("修改角色资料成功");
			return;
		}
		error("修改角色资料失败");
	}
	
	
}
